package demo.DataStrucAndAlgo.Easy_pritice;

import javax.swing.tree.TreeNode;
import java.util.*;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/11/1-11-01-19:49
 * @Description：demo.DataStrucAndAlgo.Easy_pritice
 */
   class Tree {
    int val = 0;
    Tree left = null;
    Tree right = null;
    public Tree(int val) {
      this.val = val;
    }
  }

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param root TreeNode类
     * @return int整型
     */
    Queue<Tree> queue = new ArrayDeque<>();
    List<Tree> list = new ArrayList<>();
    public int findBottomLeftValue (Tree root) {
        // write code here
        if(root == null){
            return 0;
        }
        queue.add(root);
        while(!queue.isEmpty()){
            Tree poll = queue.poll();
            if(poll.left!=null){
                queue.add(poll.left);
            }
            if(poll.right!=null){
                queue.add(poll.right);
            }
            list.add(poll);
        }
        int i = 0;
        while((i+1)*2+2<list.size()){
            i= i*2+2;
        }
        while(list.get(i)!=null){
            i++;
            return list.get(i).val;
        }
        return 0;
    }
//    int result = 1;
//    public void endOrder(Tree node,int i){
//        result = i;
//        if(node.left!=null){
//            endOrder(node.left,i+1);
//        }
//        if(node.right!=null){
//            endOrder(node.right,i+1);
//        }
//        map.put(node.val,i);
//    }
}